﻿
using Cl.Utils.Extention;
using Cl.Utils.Helper;
using Cl.Utils.Primitives.Class;
using Cl.Utils.Primitives.Const;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cl.Utils
{
    public static class GlobalUserManager
    {

        public static GlobalUserInfoModel GlobalUserInfo => JwtHelper.JWTDecodeByHttp(_httpContextAccessor);

        public static int UserId => GlobalUserInfo.UserId;
        public static string Account => GlobalUserInfo.Account;

        ///// <summary>
        ///// 方式1：通过服务器中的资源中去读取。如果服务器重启则用户信息消失；
        ///// </summary>
        //public static int UserId => (_httpContextAccessor?.HttpContext?.User.FindFirst(ClaimConst.UserId)?.Value).ToClInt();
        ///// <summary>
        ///// 方式2：不管服务器是否重启，根Token去读取。
        ///// </summary>
        //public static string Account => JwtHelper.JWTDecodeByHttp(_httpContextAccessor).Account;

        private static IHttpContextAccessor? _httpContextAccessor;
        public static void Configure(IHttpContextAccessor httpContextAccessor)//IHubContext<GlobalHub> globalHubContext
        {
            _httpContextAccessor = httpContextAccessor;
        }
    }
}
